package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.CustomerTransfer;
import cn.wolfcode.crm.mapper.CustomerMapper;
import cn.wolfcode.crm.mapper.CustomerTransferMapper;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.ICustomerTransferService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CustomerTransferServiceImpl implements ICustomerTransferService {

    @Autowired
    private CustomerTransferMapper customerTransferMapper;

    @Autowired
    private CustomerMapper customerMapper;

    @Override
    public void save(CustomerTransfer customerTransfer) {
        
        //保存的时候需要将原来的销售员改成新的销售员
        Long customerId = customerTransfer.getCustomer().getId();
        Long newSellerId = customerTransfer.getNewseller().getId();
        //用SQL更新数据库中的销售员 id
        customerMapper.updateSeller(customerId,newSellerId);


        customerTransferMapper.insert(customerTransfer);
    }

    @Override
    public CustomerTransfer get(Long id) {
        return customerTransferMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<CustomerTransfer> listAll() {
        return customerTransferMapper.selectAll();
    }

    @Override
    public void update(CustomerTransfer customerTransfer) {
        customerTransferMapper.updateByPrimaryKey(customerTransfer);
    }

    /**
     *
     * @param qo
     * @return
     */
    @Override
    public PageInfo query(QueryObject qo) {
        //int count = customerTransferMapper.selectForCount(qo);//高查询时需要参数
        //判断是否有数据
        //if (count == 0) {
        //    return new PageResult(qo.getPageSize());
        //}
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());

        //查询结果集
        List<CustomerTransfer> list = customerTransferMapper.selectForList(qo);
        //return new PageResult(qo.getCurrentPage(), count, list, qo.getPageSize());
        return new PageInfo(list);
    }
}
